﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using EncryptionLib.Users;
using EncryptionLib;
using System.Security.Cryptography;
using EncryptionLib.Encryption;
using EncryptionLib.Signature;
using System.Diagnostics;

namespace TotalCommander_v3
{
    public partial class frmEncryptProcess : Form
    {
        public string currentDir = string.Empty;
        public List<string> lstEncryptInputFiles = null;
        public SymmetricAlgorithms SymmetricAlgorithms = SymmetricAlgorithms.DES;
        public CipherMode CipherMode = CipherMode.ECB;
        public PaddingMode PaddingMode = PaddingMode.PKCS7;
        public bool Zip = true;
        public string sFileOutput = String.Empty;
        public string sEmail = String.Empty;

        public frmEncryptProcess()
        {
            InitializeComponent();
        }
        void encryptor_EncryptAsyncProgess(object sender, EncryptionProgressArgs args)
        {
            if (args.Progress == -1)
            {
                MessageBox.Show(this,
                    string.Format("An unexpected error has occurred when encrypting.\r\n\r\nError: {0}", args.ProgressExeption.Message),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            else
            {
                prbarEncrypt.Value = args.Progress;
                if (args.Progress == 100)
                {
                   // this.timer1.Enabled = false;
                    //MessageBox.Show(this, "Done!", "Success");
                    this.Close();
                }
                Debug.WriteLine(string.Format("Encrypt: {0}", args.Progress));
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (lstEncryptInputFiles == null)
            {
                this.Close();
                return;
            }
            try
            {
                Encryptor encryptor = new Encryptor();
                encryptor.Algorithm = this.SymmetricAlgorithms;
                encryptor.CipherMode = this.CipherMode;
                encryptor.OutputFile = sFileOutput;
                encryptor.PaddingMode = this.PaddingMode;
                encryptor.Zipped = this.Zip;

                // to make sure we got FILES only.
                foreach (string lvi in lstEncryptInputFiles)
                {
                    string sPath = Path.Combine(currentDir, lvi);
                    if (Directory.Exists(sPath))
                    {
                        foreach (string sFile in Directory.GetFiles(sPath, "*.*", SearchOption.AllDirectories))
                        {
                            if (!encryptor.Files.Contains(sFile)
                                && File.Exists(sFile))
                                encryptor.Files.Add(sFile);
                        }
                    }
                    if (!encryptor.Files.Contains(sPath) && File.Exists(sPath))
                        encryptor.Files.Add(sPath);
                }

                // encrpyt with current user
                encryptor.Reciever = (UserData)frmMain.m_userManager.GetUserByEmail(this.sEmail);

                encryptor.EncryptAsyncProgess += new EncryptionProgressHandler(encryptor_EncryptAsyncProgess);
                encryptor.EncryptAsync(this);
                this.timer1.Enabled = false;
                //encryptor.Encrypt();

                ///MessageBox.Show(this, "Successfully!", "Congrats", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                this.timer1.Enabled = false;
                MessageBox.Show(this, string.Format("Error:\r\n{0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void frmProcess_Load(object sender, EventArgs e)
        {

        }
    }
}
